package com.bishe.bishe.Controller;

import com.bishe.bishe.Service.SpeciesService;
import com.bishe.bishe.entity.PageResult;
import com.bishe.bishe.entity.WxEntity;
import com.bishe.bishe.entity.WxSpecies;
import com.bishe.bishe.pojo.Species;
import com.bishe.bishe.pojo.Types;
import com.bishe.bishe.pojo.Unit;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/species")
public class SpeciesController {
    @Autowired
    private SpeciesService speciesService;

    @GetMapping("/findall")
    public List <Species> findall() {
        return speciesService.findall();
    }
    @GetMapping("/findallByWx")
    public Object findallByWx() {
        List<Species> findall = speciesService.findall();
        return wxDate(findall);
    }
    @GetMapping("/findByWx")
    public Object findByWx() {
        List<Species> findall = speciesService.findall();
        List<Species> species=new ArrayList<>();
        for (int i=0;i<findall.size();i++){
            if (findall.get(i).getSpeciesState().equals("已上架")&&findall.get(i).getTypes().getTypeState()==1){
                species.add(findall.get(i));//将已上架的放在species集合中
            }
        }
        return wxDate(species);
    }

    public List<WxEntity> wxDate(List<Species> speciesist){
        List<WxEntity> wxEntities=new ArrayList <>();
        for (int y=0;y<speciesist.size();y++){
            String url="../../images/";
            List<WxSpecies> wxSpecies=new ArrayList <>();
            WxEntity wxEntity=new WxEntity();
            wxEntity.setTypeId(speciesist.get(y).getTypes().getTypeId());
            wxEntity.setTypeName(speciesist.get(y).getTypes().getTypeName());
            WxSpecies wxSpecies1=new WxSpecies();
            wxSpecies1.setWxSpeciesId(speciesist.get(y).getSpeciesId());
            url=url+speciesist.get(y).getSpeciesImg();
            wxSpecies1.setWxSpeciesImg(url);
            wxSpecies1.setWxSpeciesName(speciesist.get(y).getSpeciesName());
            wxSpecies1.setWxSpeciesIntroduce(speciesist.get(y).getSpeciesIntroduce());
            wxSpecies1.setWxSpeciesPrice(speciesist.get(y).getSpeciesPrice());
            wxSpecies1.setWxSpeciesTotal(speciesist.get(y).getSpeciesTotal());
            wxSpecies1.setWxSpeciesUnit(speciesist.get(y).getUnit().getUnitName());
            wxSpecies1.setWxSpeciesState(speciesist.get(y).getSpeciesState());
            wxSpecies.add(wxSpecies1);
            for (int a=y+1;a<speciesist.size();a++){
                String url1="../../images/";
                if (speciesist.get(a).getTypes().getTypeId()==speciesist.get(y).getTypes().getTypeId()){
                    WxSpecies wxSpecies2=new WxSpecies();
                    wxSpecies2.setWxSpeciesId(speciesist.get(a).getSpeciesId());
                    url1=url1+speciesist.get(a).getSpeciesImg();
                    wxSpecies2.setWxSpeciesImg(url1);
                    wxSpecies2.setWxSpeciesName(speciesist.get(a).getSpeciesName());
                    wxSpecies2.setWxSpeciesIntroduce(speciesist.get(a).getSpeciesIntroduce());
                    wxSpecies2.setWxSpeciesPrice(speciesist.get(a).getSpeciesPrice());
                    wxSpecies2.setWxSpeciesTotal(speciesist.get(a).getSpeciesTotal());
                    wxSpecies2.setWxSpeciesUnit(speciesist.get(a).getUnit().getUnitName());
                    wxSpecies2.setWxSpeciesState(speciesist.get(a).getSpeciesState());
                    wxSpecies.add(wxSpecies2);
                    speciesist.remove(a);
                    a--;
                }
            }
            wxEntity.setWxSpecies(wxSpecies);
            wxEntities.add(wxEntity);
        }

        return wxEntities;
    }

    @GetMapping("/findPage")/*分页显示所有这样该方法请求只能用get*/
    public PageInfo<Species> findPage(int page, int size) {
        /*调用该方法需要传参数，page和size*/
        return speciesService.findPage(page, size);
    }

    @RequestMapping("/findList")/*模糊查询参数名字为name*/
    public List <Species> findList(@RequestBody Map searchMap) {
        return speciesService.findList(searchMap);
    }

    @RequestMapping("/findPage")/*分页+模糊查询，和上一个有区别，这个需要map数据*/
    public PageInfo <Species> findPage(@RequestBody Map searchMap, int page, int size) {
        return speciesService.findPage(searchMap, page, size);
    }

    @RequestMapping("/findspecies")
    public Species findspecies(@Validated @RequestParam(name = "speciesId",required = false) Integer speciesId) {
        return speciesService.findById(speciesId);
    }

    @RequestMapping("/findspeciesname")
    public int findspeciesname(@Validated @RequestParam(name = "speciesName",required = false) String speciesName) {
        List <Species> byName = speciesService.findByName(speciesName);
        if (byName.size()>0){
            return byName.get(0).getSpeciesId();
        }else {
            return 0;
        }
    }
    @RequestMapping("/findspeciesnameforWx")
    public Object findspeciesnameforWx(@Validated @RequestParam(name = "speciesName",required = false) String speciesName) {
        System.out.println("*******"+speciesName);
        List <Species> byName = speciesService.findByNames(speciesName);
        return wxDate(byName);
    }


    @RequestMapping("/delete")
    public int delete(@Validated @RequestParam(name = "speciesId",required = false) Integer speciesId) {
        String speciesState="已取消";
        return speciesService.updatestate(speciesId,speciesState);
    }

    /****************/
    public Map uploadImg(MultipartFile file) {
        return  speciesService.uploadImg(file);
    }
    /**********/
    @RequestMapping("/insert")
    public int insert(@Validated @RequestParam(name = "speciesName",required = false) String speciesName,@Validated @RequestParam(name = "speciesPrice",required = false) Double speciesPrice,@Validated @RequestParam(name = "speciesIntroduce",required = false) String speciesIntroduce,@Validated @RequestParam(name = "typeName",required = false) String typeName,@Validated @RequestParam(name = "unitName",required = false) String unitName,@Validated @RequestParam(name="multipartFile",required = false) MultipartFile multipartFile) {
        Species species=new Species();
        Types types=new Types();
        Unit unit=new Unit();
        unit.setUnitName(unitName);
        types.setTypeName(typeName);

        Map map = uploadImg(multipartFile);
        species.setSpeciesTotal(0);
        species.setSpeciesName(speciesName);
        species.setSpeciesIntroduce(speciesIntroduce);
        species.setSpeciesPrice(speciesPrice);
        species.setSpeciesImg(map.get("imgUrl").toString());
        species.setTypes(types);
        species.setUnit(unit);
        species.setSpeciesState("未上架");
        return speciesService.insert(species);
    }
    @RequestMapping("/update")
    public int update(@Validated @RequestParam(name = "speciesId",required = false) Integer speciesId,@Validated @RequestParam(name = "speciesTotal",required = false) Integer speciesTotal,@Validated @RequestParam(name = "speciesName",required = false) String speciesName,@Validated @RequestParam(name = "speciesPrice",required = false) Double speciesPrice,@Validated @RequestParam(name = "speciesIntroduce",required = false) String speciesIntroduce,@Validated @RequestParam(name = "typeName",required = false) String typeName,@Validated @RequestParam(name = "unitName",required = false) String unitName) {
        Species species=new Species();
        Types types=new Types();
        Unit unit=new Unit();
        unit.setUnitName(unitName);
        types.setTypeName(typeName);
        species.setSpeciesId(speciesId);
        species.setSpeciesTotal(speciesTotal);
        species.setSpeciesName(speciesName);
        species.setSpeciesIntroduce(speciesIntroduce);
        species.setSpeciesPrice(speciesPrice);
        species.setTypes(types);
        species.setUnit(unit);
        species.setSpeciesState("未上架");
        return speciesService.update(species);
    }
    @RequestMapping("/batch")
    public int batchupdate(@Validated @RequestBody List<Integer> idlist,@Validated @RequestParam(name = "speciesState",required = false) String speciesState){
        for (int i = 0; i < idlist.size(); i++) {
            speciesService.updatestate(Integer.valueOf(idlist.get(i)),speciesState);
        }
        return 1;
    }

    @RequestMapping("/updateState")
    public int updateState(@Validated @RequestParam(name = "speciesId",required = false) Integer speciesId) {
        String speciesState="未上架";
        return speciesService.updatestate(speciesId,speciesState);
    }
    @RequestMapping("/updateState1")
    public int updateState1(@Validated @RequestParam(name = "speciesId",required = false) Integer speciesId) {
        String speciesState="已上架";
        return speciesService.updatestate(speciesId,speciesState);
    }

    @RequestMapping("/updateImg")
    public int updateImg(@Validated @RequestParam(name = "speciesId",required = false) Integer speciesId,@Validated @RequestParam(name = "speciesTotal",required = false) Integer speciesTotal,@Validated @RequestParam(name = "speciesName",required = false) String speciesName,@Validated @RequestParam(name = "speciesPrice",required = false) Double speciesPrice,@Validated @RequestParam(name = "speciesIntroduce",required = false) String speciesIntroduce,@Validated @RequestParam(name = "typeName",required = false) String typeName,@Validated @RequestParam(name = "unitName",required = false) String unitName,@Validated @RequestParam(name="multipartFile",required = false) MultipartFile multipartFile) {
        Species species=new Species();
        Types types=new Types();
        Unit unit=new Unit();
        unit.setUnitName(unitName);
        types.setTypeName(typeName);

        Map map = uploadImg(multipartFile);
        species.setSpeciesId(speciesId);
        species.setSpeciesTotal(speciesTotal);
        species.setSpeciesName(speciesName);
        species.setSpeciesIntroduce(speciesIntroduce);
        species.setSpeciesPrice(speciesPrice);
        species.setSpeciesImg(map.get("imgUrl").toString());
        species.setTypes(types);
        species.setUnit(unit);
        species.setSpeciesState("未上架");
        return speciesService.updateImg(species);
    }

    @Value("${absoluteImgPath}")
    String absoluteImgPath;
    @Value("${absoluteImgPath1}")
    String absoluteImgPath1;
    @RequestMapping("/clearImg")
    public  void deleteFile(){
        List<String> strings = speciesService.listImg();//数据库图片名称+后缀
        List<String> list = new ArrayList<>();//文件夹下所有文件名
        List<String> clear = new ArrayList<>();//多余的图片
        File file = new File(absoluteImgPath);
        File[] files = file.listFiles();
        for (File img:files) {
            list.add(img.getName());
        }
        for (String t : list) {
            if (!strings.contains(t)) {
                clear.add(t);
            }
        }
        for (String t1:clear){
           File file1=new File(absoluteImgPath+t1);
           file1.delete();
            File file2=new File(absoluteImgPath1+t1);
            file2.delete();
        }
    }

}
